package com.rpay.dao.config;

import com.rpay.common.factory.YamlPropertySourceFactory;
import com.rpay.common.util.LoggerUtil;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;
import java.util.Properties;


/**
 * @author jonnyLee
 */
@Configuration
@PropertySource(value = "classpath:dao.yml", factory = YamlPropertySourceFactory.class)
@EnableTransactionManagement(proxyTargetClass = true)
@AutoConfigureAfter({DataBaseConfiguration.class})
public class MybatisConfiguration {


    @Bean(name = "readSqlSession")
    public SqlSessionTemplate readSqlSession(@Qualifier("readDataSource") DataSource dataSource, MybatisProperties properties) {
        return new SqlSessionTemplate(this.factory(dataSource, properties));
    }

    @Primary
    @Bean(name = "writeSqlSession")
    public SqlSessionTemplate writeSqlSessionFactoryBean(@Qualifier("writeDataSource") DataSource dataSource, MybatisProperties properties) {
        return new SqlSessionTemplate(this.factory(dataSource, properties));
    }

    @Bean(name = "transactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("writeDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean
    @ConditionalOnMissingBean
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("readDataSource") DataSource dataSource, MybatisProperties properties) {
        return new SqlSessionTemplate(this.factory(dataSource, properties));
    }

    private SqlSessionFactory factory(DataSource dataSource, MybatisProperties properties) {
        try {
            final SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
            factoryBean.setDataSource(dataSource);
            if (properties.getConfigLocation() != null) {
                PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
                factoryBean.setConfigLocation(resolver.getResource(properties.getConfigLocation()));
            }

            if (StringUtils.hasLength(properties.getTypeAliasesPackage())) {
                factoryBean.setTypeAliasesPackage(properties.getTypeAliasesPackage());
            }
            if (!ObjectUtils.isEmpty(properties.resolveMapperLocations())) {
                factoryBean.setMapperLocations(properties.resolveMapperLocations());
            }
//            factoryBean.setPlugins(new Interceptor[] { this.pageHelper()});


            return factoryBean.getObject();
        } catch (Exception ex) {
            LoggerUtil.error("...... configuration sqlSession is error......", ex);
        }
        return null;
    }

//    @Bean
//    public PageHelper pageHelper(DataSource dataSource) {
//        LoggerUtil.info("注册 pageHelper 拦截器");
//        PageHelper pageHelper = new PageHelper();
//        Properties p = new Properties();
//        p.setProperty("helperDialect", "helperDialect");
//        p.setProperty("offsetAsPageNum", "true");
//        p.setProperty("rowBoundsWithCount", "true");
//        p.setProperty("reasonable", "true");
//        p.setProperty("supportMethodsArguments", "true");
//        pageHelper.setProperties(p);
//        return pageHelper;
//    }


}